<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\tree_table\util\tttfilter;

use builder\helper\H5;

/**
 * layui下拉框
 */
class SelectLayui implements FilterControlInterface
{
    /**
     * @var array 选项
     */
    private $_options = [
        'control' => 'select_layui',
        'label' => '',
        'placeholder' => '请输入',
        'default' => '',
        'options' => [],
        'help' => '',
        'class' => '',
        'style' => '',
        'attribute' => '',
        'disabled' => false,
        'search' => false,
        'search_caseSensitive' => 'false',
        'search_fuzzy' => 'false',
        'creatable' => false,
        'unspread' => false,
    ];

    /**
     * @param string $label 标签
     * @return $this
     */
    public function label($label)
    {
        $this->_options['label'] = (string)$label;
        return $this;
    }

    /**
     * @param string $placeholder 内容提示
     * @return $this
     */
    public function placeholder($placeholder)
    {
        $this->_options['placeholder'] = (string)$placeholder;
        return $this;
    }

    /**
     * 默认值
     * @param string $default
     * @return $this
     */
    public function defaultValue($default = '')
    {
        $this->_options['default'] = (string)$default;
        return $this;
    }

    /**
     * 选项
     * @param array $options
     * ```php
     * // label: 选项名  value: 选项值  disabled: 是否禁用该选项
     * $this->options([
     *     ['value' => 1, 'label'=> '钻石', 'disabled' => false],
     *     ['value' => 2, 'label'=> '黄金', 'disabled' => false],
     *     ['value' => 3, 'label'=> '白银', 'disabled' => false],
     * ]);
     * ```
     * @return $this
     */
    public function options(array $options): SelectLayui
    {
        $this->_options['options'] = $options;
        return $this;
    }

    /**
     * @param string|array $class CSS类
     * @return $this
     */
    public function uiClass($class)
    {
        $this->_options['class'] = $class;
        return $this;
    }

    /**
     * @param string|array $style CSS样式
     * @return $this
     */
    public function style($style)
    {
        $this->_options['style'] = $style;
        return $this;
    }

    /**
     * @param array|string $attribute HTML标签属性
     * @return $this
     */
    public function attribute($attribute): SelectLayui
    {
        $this->formatHtmlAttribute($attribute);
        $this->_options['attribute'] = $attribute;

        return $this;
    }

    /**
     * 帮助文字
     * @param string $help
     * @return $this
     */
    public function help($help)
    {
        $this->_options['help'] = (string)$help;
        return $this;
    }

    /**
     * 当前开启搜索时，是否允许创建新的option。
     * @param bool $bool
     * @return $this
     */
    public function creatable(bool $bool): SelectLayui
    {
        $this->_options['creatable'] = $bool;
        return $this;
    }

    /**
     * 当前开启搜索时，是否开启模糊匹配
     * @param bool $bool
     * @return $this
     */
    public function search_fuzzy(bool $bool): SelectLayui
    {
        $this->_options['search_fuzzy'] = $bool ? 'true' : 'false';
        return $this;
    }

    /**
     * 当前开启搜索时，是否区分大小写
     * @param bool $bool
     * @return $this
     */
    public function search_caseSensitive(bool $bool): SelectLayui
    {
        $this->_options['search_caseSensitive'] = $bool ? 'true' : 'false';
        return $this;
    }

    /**
     * 是否开启搜索
     * @param bool $bool
     * @return $this
     */
    public function search(bool $bool): SelectLayui
    {
        $this->_options['search'] = $bool;
        return $this;
    }

    /**
     * 是否禁用控件
     * @param bool $bool
     * @return $this
     */
    public function disabled(bool $bool): SelectLayui
    {
        $this->_options['disabled'] = $bool;
        return $this;
    }

    /**
     * 折叠控件
     * @return $this
     */
    public function unspread()
    {
        $this->_options['unspread'] = true;
        return $this;
    }

    /**
     * @return array
     */
    public function _toArray(): array
    {
        $options = $this->_options;

        $addonAttrs = [];
        if ($options['disabled']) $addonAttrs['disabled'] = '';
        if ($options['search']) {
            $addonAttrs['lay-search'] = "{caseSensitive:{$options['search_caseSensitive']}, fuzzy: {$options['search_fuzzy']}}";
        }
        if ($options['creatable']) $addonAttrs['lay-creatable'] = '';

        if (!empty($addonAttrs)) {
            $this->formatHtmlAttribute($addonAttrs);
        } else {
            $addonAttrs = '';
        }

        $options['attribute'] .= $addonAttrs;

        return $options;
    }

    /**
     * 格式化html属性
     * @param array|string $attribute
     */
    protected function formatHtmlAttribute(&$attribute)
    {
        if (is_array($attribute)) {
            $attribute = H5::renderTagAttributes($attribute);
        } else {
            $attribute = ' ' . ltrim($attribute);
        }
    }
}